package com.guli.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> treeList() {
        //查询分类表的所有数据
        List<CategoryEntity> allList = baseMapper.selectList(null);
//        jvm调用
//        将一级分类做一个筛选
//        首先通过filter进行一段过滤
        List<CategoryEntity> list = allList.stream().filter(item -> {
//            结果为真时返回
            return item.getParentCid().equals(0l);
        }).map(item -> {
//            每一个item就是一个一级分类
//            设置二级分类并获取
//            Long catId = item.getCatId();
//            List<CategoryEntity> children = getChildren(catId, allList);

            item.setChildren(getChildren(item.getCatId(), allList));
            return item;
        }).collect(Collectors.toList());
        return list;
    }

//    在后面写一个私有的方法
    private List<CategoryEntity> getChildren(Long catId,List<CategoryEntity> allList){
        List<CategoryEntity> children = allList.stream().filter(item -> {
            return item.getParentCid().equals(catId);
        }).map(item -> {
//            item就是个二级分类
            item.setChildren(getChildren(item.getCatId(),allList));
            return item;
        }).collect(Collectors.toList());

        return children;
    }




/*    @Override
    public List<CategoryEntity> treeList() {

        //1.查询一级分类
        List<CategoryEntity> l1List = baseMapper.selectList(null);

//        遍历所有的一级分类,得到分类编号,就有了二级分类的pid
//        遍历一级分类 通过id查询二级分类的集合
        for (CategoryEntity l1 : l1List) {
//            得到一级分类的主键
            Long catId = l1.getCatId();

            List<CategoryEntity> l2List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
            l1.setChildren(l2List);
//            遍历每个二级分类查询三级分类集合
            for (CategoryEntity l2 : l2List) {
                List<CategoryEntity> l3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l2.getCatId()));
                l2.setChildren(l3List);
            }
            return l1List;
        }


        return null;
    }   */

}